from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QStyleFactory, QMessageBox
from common.common import Common


class ShowTabLight:
    def __init__(self, ui, ptr):
        self.ui = ui
        self.ptr = ptr
        self.side_light1_model_control = None
        self.side_light1_model_show = None
        self.side_light2_model_control = None
        self.side_light2_model_show = None
        self.side_light3_model_control = None
        self.side_light3_model_show = None
        self.side_light4_model_control = None
        self.side_light4_model_show = None
        self.fill_light1_model_control = None
        self.fill_light1_model_show = None
        self.fill_light2_model_control = None
        self.fill_light2_model_show = None

    # init tab base(tab)
    def init_ui(self):
        self.init_tab_motor_group_side_light1()
        self.init_tab_motor_group_side_light2()
        self.init_tab_motor_group_side_light3()
        self.init_tab_motor_group_side_light4()
        self.init_tab_motor_group_fill_light1()
        self.init_tab_motor_group_fill_light2()
        self.init_connect()
        pass

    def init_connect(self):
        self.ui.pushButton_side_light1_control.clicked.connect(
            lambda: self.btn_side_light1_control_clicked("side light1 control"))
        self.ui.pushButton_side_light2_control.clicked.connect(
            lambda: self.btn_side_light3_control_clicked("side light2 control"))
        self.ui.pushButton_side_light3_control.clicked.connect(
            lambda: self.btn_side_light3_control_clicked("side light3 control"))
        self.ui.pushButton_side_light4_control.clicked.connect(
            lambda: self.btn_side_light4_control_clicked("side light4 control"))
        self.ui.pushButton_fill_light1_control.clicked.connect(
            lambda: self.btn_fill_light1_control_clicked("fill light1 control"))
        self.ui.pushButton_fill_light2_control.clicked.connect(
            lambda: self.btn_fill_light2_control_clicked("fill light2 control"))

    @staticmethod
    def init_tree_side_light1_control(model):
        # set item
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('闪烁频率(次/秒)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))
        item.child(0, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('闪烁时间S')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))
        item.child(1, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('颜色(1白2红3蓝0关)')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('0'))

    @staticmethod
    def init_tree_side_light1_show(model):
        # set item
        item = QStandardItem('状态')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('电流mA')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    def init_tab_motor_group_side_light1(self):
        # set table header
        self.side_light1_model_control = QStandardItemModel(self.ptr)
        self.side_light1_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.side_light1_model_show = QStandardItemModel(self.ptr)
        self.side_light1_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_side_light1_control(self.side_light1_model_control)
        self.init_tree_side_light1_show(self.side_light1_model_show)

        self.ui.treeView_side_light1_control.setModel(self.side_light1_model_control)
        self.ui.treeView_side_light1_show.setModel(self.side_light1_model_show)

        # 调整第一列的宽度
        self.ui.treeView_side_light1_control.header().resizeSection(0, 220)
        self.ui.treeView_side_light1_show.header().resizeSection(0, 160)
        # 设置成有虚线连接的方式
        self.ui.treeView_side_light1_control.setStyle(QStyleFactory.create("windows"))
        self.ui.treeView_side_light1_show.setStyle(QStyleFactory.create("windows"))
        # 完全展开
        self.ui.treeView_side_light1_control.expandAll()
        self.ui.treeView_side_light1_show.expandAll()

    @staticmethod
    def init_tree_side_light2_control(model):
        # set item
        item = QStandardItem('控制')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('闪烁频率(次/秒)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))
        item.child(0, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('闪烁时间S')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))
        item.child(1, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('颜色(1白2红3蓝0关)')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('0'))

    @staticmethod
    def init_tree_side_light2_show(model):
        # set item
        item = QStandardItem('状态')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('电流mA')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    def init_tab_motor_group_side_light2(self):
        # set table header
        self.side_light2_model_control = QStandardItemModel(self.ptr)
        self.side_light2_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.side_light2_model_show = QStandardItemModel(self.ptr)
        self.side_light2_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_side_light2_control(self.side_light2_model_control)
        self.init_tree_side_light2_show(self.side_light2_model_show)

        self.ui.treeView_side_light2_control.setModel(self.side_light2_model_control)
        self.ui.treeView_side_light2_show.setModel(self.side_light2_model_show)

        # 调整第一列的宽度
        self.ui.treeView_side_light2_control.header().resizeSection(0, 220)
        self.ui.treeView_side_light2_show.header().resizeSection(0, 160)
        # 设置成有虚线连接的方式
        self.ui.treeView_side_light2_control.setStyle(QStyleFactory.create("windows"))
        self.ui.treeView_side_light2_show.setStyle(QStyleFactory.create("windows"))
        # 完全展开
        self.ui.treeView_side_light2_control.expandAll()
        self.ui.treeView_side_light2_show.expandAll()

    @staticmethod
    def init_tree_side_light3_control(model):
        # set item
        item = QStandardItem('控制')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('闪烁频率(次/秒)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))
        item.child(0, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('闪烁时间S')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))
        item.child(1, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('颜色(1白2红3蓝0关)')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('0'))

    @staticmethod
    def init_tree_side_light3_show(model):
        # set item
        item = QStandardItem('状态')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('电流mA')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    def init_tab_motor_group_side_light3(self):
        # set table header
        self.side_light3_model_control = QStandardItemModel(self.ptr)
        self.side_light3_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.side_light3_model_show = QStandardItemModel(self.ptr)
        self.side_light3_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_side_light3_control(self.side_light3_model_control)
        self.init_tree_side_light3_show(self.side_light3_model_show)

        self.ui.treeView_side_light3_control.setModel(self.side_light3_model_control)
        self.ui.treeView_side_light3_show.setModel(self.side_light3_model_show)

        # 调整第一列的宽度
        self.ui.treeView_side_light3_control.header().resizeSection(0, 220)
        self.ui.treeView_side_light3_show.header().resizeSection(0, 160)
        # 设置成有虚线连接的方式
        self.ui.treeView_side_light3_control.setStyle(QStyleFactory.create("windows"))
        self.ui.treeView_side_light3_show.setStyle(QStyleFactory.create("windows"))
        # 完全展开
        self.ui.treeView_side_light3_control.expandAll()
        self.ui.treeView_side_light3_show.expandAll()

    @staticmethod
    def init_tree_side_light4_control(model):
        # set item
        item = QStandardItem('控制')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('闪烁频率(次/秒)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))
        item.child(0, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('闪烁时间S')
        item.appendRow(item_child)
        item.setChild(1, 1, QStandardItem('[Object]'))
        item.child(1, 1).setEnabled(False)

        # 添加子条目
        item_child = QStandardItem('颜色(1白2红3蓝0关)')
        item.appendRow(item_child)
        item.setChild(2, 1, QStandardItem('0'))

    @staticmethod
    def init_tree_side_light4_show(model):
        # set item
        item = QStandardItem('状态')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('电流mA')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    def init_tab_motor_group_side_light4(self):
        # set table header
        self.side_light4_model_control = QStandardItemModel(self.ptr)
        self.side_light4_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.side_light4_model_show = QStandardItemModel(self.ptr)
        self.side_light4_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_side_light4_control(self.side_light4_model_control)
        self.init_tree_side_light4_show(self.side_light4_model_show)

        self.ui.treeView_side_light4_control.setModel(self.side_light4_model_control)
        self.ui.treeView_side_light4_show.setModel(self.side_light4_model_show)

        # 调整第一列的宽度
        self.ui.treeView_side_light4_control.header().resizeSection(0, 220)
        self.ui.treeView_side_light4_show.header().resizeSection(0, 160)
        # 设置成有虚线连接的方式
        self.ui.treeView_side_light4_control.setStyle(QStyleFactory.create("windows"))
        self.ui.treeView_side_light4_show.setStyle(QStyleFactory.create("windows"))
        # 完全展开
        self.ui.treeView_side_light4_control.expandAll()
        self.ui.treeView_side_light4_show.expandAll()

    @staticmethod
    def init_tree_fill_light1_control(model):
        # set item
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('开关(0关1开)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    @staticmethod
    def init_tree_fill_light1_show(model):
        # set item
        item = QStandardItem('状态')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('电流mA')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    def init_tab_motor_group_fill_light1(self):
        # set table header
        self.fill_light1_model_control = QStandardItemModel(self.ptr)
        self.fill_light1_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.fill_light1_model_show = QStandardItemModel(self.ptr)
        self.fill_light1_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_fill_light1_control(self.fill_light1_model_control)
        self.init_tree_fill_light1_show(self.fill_light1_model_show)

        self.ui.treeView_fill_light1_control.setModel(self.fill_light1_model_control)
        self.ui.treeView_fill_light1_show.setModel(self.fill_light1_model_show)

        # 调整第一列的宽度
        self.ui.treeView_fill_light1_control.header().resizeSection(0, 180)
        self.ui.treeView_fill_light1_show.header().resizeSection(0, 160)
        # 设置成有虚线连接的方式
        self.ui.treeView_fill_light1_control.setStyle(QStyleFactory.create("windows"))
        self.ui.treeView_fill_light1_show.setStyle(QStyleFactory.create("windows"))
        # 完全展开
        self.ui.treeView_fill_light1_control.expandAll()
        self.ui.treeView_fill_light1_show.expandAll()
        pass

    @staticmethod
    def init_tree_fill_light2_control(model):
        # set item
        item = QStandardItem('控制')
        model.appendRow(item)
        # model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('开关(0关1开)')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    @staticmethod
    def init_tree_fill_light2_show(model):
        # set item
        item = QStandardItem('状态')
        model.appendRow(item)
        model.setItem(0, 1, QStandardItem('[Object]'))
        # 添加子条目
        item_child = QStandardItem('电流mA')
        item.appendRow(item_child)
        item.setChild(0, 1, QStandardItem('[Object]'))

    def init_tab_motor_group_fill_light2(self):
        # set table header
        self.fill_light2_model_control = QStandardItemModel(self.ptr)
        self.fill_light2_model_control.setHorizontalHeaderLabels(['item', 'value'])
        self.fill_light2_model_show = QStandardItemModel(self.ptr)
        self.fill_light2_model_show.setHorizontalHeaderLabels(['item', 'value'])
        self.init_tree_fill_light2_control(self.fill_light2_model_control)
        self.init_tree_fill_light2_show(self.fill_light2_model_show)

        self.ui.treeView_fill_light2_control.setModel(self.fill_light2_model_control)
        self.ui.treeView_fill_light2_show.setModel(self.fill_light2_model_show)

        # 调整第一列的宽度
        self.ui.treeView_fill_light2_control.header().resizeSection(0, 180)
        self.ui.treeView_fill_light2_show.header().resizeSection(0, 160)
        # 设置成有虚线连接的方式
        self.ui.treeView_fill_light2_control.setStyle(QStyleFactory.create("windows"))
        self.ui.treeView_fill_light2_show.setStyle(QStyleFactory.create("windows"))
        # 完全展开
        self.ui.treeView_fill_light2_control.expandAll()
        self.ui.treeView_fill_light2_show.expandAll()
        pass

    def btn_side_light1_control_clicked(self, info):
        print(info)
        # side light1 color
        value_str = self.side_light1_model_control.item(0).child(2, 1).text()
        value_i = int(value_str)
        if value_i == 1:  # white
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2A, 0x02, 0x01, 0x00, 0x00, 0xA5])
        elif value_i == 2:  # red
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2A, 0x02, 0x02, 0x00, 0x00, 0xA5])
        elif value_i == 3:  # blue
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2A, 0x02, 0x03, 0x00, 0x00, 0xA5])
        elif value_i == 0:  # turn off
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2A, 0x02, 0x00, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-3", QMessageBox.Cancel)

    def btn_side_light2_control_clicked(self, info):
        print(info)
        # side light1 color
        value_str = self.side_light2_model_control.item(0).child(2, 1).text()
        value_i = int(value_str)
        if value_i == 1:  # white
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2B, 0x02, 0x01, 0x00, 0x00, 0xA5])
        elif value_i == 2:  # red
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2B, 0x02, 0x02, 0x00, 0x00, 0xA5])
        elif value_i == 3:  # blue
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2B, 0x02, 0x03, 0x00, 0x00, 0xA5])
        elif value_i == 0:  # turn off
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2B, 0x02, 0x00, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-3", QMessageBox.Cancel)

    def btn_side_light3_control_clicked(self, info):
        print(info)
        value_str = self.side_light3_model_control.item(0).child(2, 1).text()
        value_i = int(value_str)
        if value_i == 1:  # white
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2C, 0x02, 0x01, 0x00, 0x00, 0xA5])
        elif value_i == 2:  # red
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2C, 0x02, 0x02, 0x00, 0x00, 0xA5])
        elif value_i == 3:  # blue
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2C, 0x02, 0x03, 0x00, 0x00, 0xA5])
        elif value_i == 0:  # turn off
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2C, 0x02, 0x00, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-3", QMessageBox.Cancel)

    def btn_side_light4_control_clicked(self, info):
        print(info)
        value_str = self.side_light4_model_control.item(0).child(2, 1).text()
        value_i = int(value_str)
        if value_i == 1:  # white
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2D, 0x02, 0x01, 0x00, 0x00, 0xA5])
        elif value_i == 2:  # red
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2D, 0x02, 0x02, 0x00, 0x00, 0xA5])
        elif value_i == 3:  # blue
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2D, 0x02, 0x03, 0x00, 0x00, 0xA5])
        elif value_i == 0:  # turn off
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x2D, 0x02, 0x00, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0-3", QMessageBox.Cancel)

    def btn_fill_light1_control_clicked(self, info):
        print(info)
        # control fill light1
        value_str = self.fill_light1_model_control.item(0).child(0, 1).text()
        value_i = int(value_str)
        if value_i == 1:
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x28, 0x02, 0x01, 0x00, 0x00, 0xA5])
        elif value_i == 0:
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x28, 0x02, 0x00, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0，1", QMessageBox.Cancel)

    def btn_fill_light2_control_clicked(self, info):
        print(info)
        value_str = self.fill_light2_model_control.item(0).child(0, 1).text()
        value_i = int(value_str)
        if value_i == 1:
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x29, 0x02, 0x01, 0x00, 0x00, 0xA5])
        elif value_i == 0:
            Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x29, 0x02, 0x00, 0x00, 0x00, 0xA5])
        else:
            QMessageBox.warning(self.ptr, "Error", "非法输入值，只接受0，1", QMessageBox.Cancel)

    def update_side_light1(self, value, child_row, child_col):
        parent_item = self.side_light1_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_side_light2(self, value, child_row, child_col):
        parent_item = self.side_light2_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_side_light3(self, value, child_row, child_col):
        parent_item = self.side_light3_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_side_light4(self, value, child_row, child_col):
        parent_item = self.side_light4_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_fill_light1(self, value, child_row, child_col):
        parent_item = self.fill_light1_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))

    def update_fill_light2(self, value, child_row, child_col):
        parent_item = self.fill_light2_model_show.item(0)
        parent_item.child(child_row, child_col).setText(str(value))